<template>
	<view class="box">
		<view class="persondata" v-for="(val,index) in persondata">
			<view class="top">
				<view class="img_box">
					<image :src="val.avatarUrl" mode=""></image>
				</view>
				<view class="name">{{val.nickName}}</view>
			</view>
			<view class=""></view>
			<view class="update">
				<view class="up_ul">
					<view class="up_li clear">
						<view class="up_name">
							昵称
						</view>

						<view class="inp">
							<input type="text" v-model="val.nickName" />
						</view>

					</view>

					<view class="xian"></view>

					<view class="up_li clear">
						<view class="up_name">
							手机
						</view>

						<view class="inp">
							<input type="text" v-model="phone" placeholder="请输入手机号" style="font-size: 0.8rem;"/>
						</view>
					</view>

					<view class="xian"></view>

					<view class="up_li clear">
						<view class="up_name">
							性别
						</view>

						<view class="inp">
							<radio-group name="" @change="radiochecked">
								<label>
									<radio value="男" name="nan" style="transform: scale(0.7);"/><text>男</text>
								</label>
								<label>
									<radio value="女" name="nv" style="transform: scale(0.7);"/><text>女</text>
								</label>
							</radio-group>
						</view>
					</view>

					<view class="xian"></view>

					<view class="up_li clear">
						<view class="up_name">
							生日
						</view>

						<view class="inp">
							<view>
								<picker mode="date" :value="date" :start="startDate" :end="endDate"
									@change="bindDateChange">
									<view class="uni-input" style="font-size: 0.85rem;">{{date}}</view>
								</picker>
							</view>
						</view>
					</view>

					
				</view>
			</view>

			<view class="savedata">
				<view class="but_box">
					<button type="default" @tap="update">保存</button>
				</view>
			</view>
		</view>



	</view>
</template>

<script>
	import {
		logincheck,
		addUser
	} from "../../static/service/center/logincheck.js"
	
	import { isEmpytInput } from "../../static/utils/isEmpyt.js"
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				persondata: [
					{
						nickName: '',
						avatarUrl: '',
						phone: '',
						gender: '',
						birthday: '',
					}
				],
				date: '请选择日期',
				phone: '1'
			}
		},
		onShow() {
			let openid = uni.getStorageSync('openid')
			let that = this
			this.db('user').where({
					_openid: openid
				}).get()
				.then((res) => {
					for (let key in res.data[0]) {
						//console.log(that.persondata[0][key],key)
						if(this.persondata[0][key] !=undefined){
							console.log(key)
							this.persondata[0][key] = res.data[0][key]
						}
					}
					//console.log(this.persondata)
					
				})
		},
		watch:{
			phone(){
				let phone1 = this.phone
				let regPhone = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
				if(!regPhone.test(phone1)){
					console.log("错误")
					return
				}else{
					console.log("对")
				}
				console.log(123)
				
				this.persondata[0].phone = phone1
				console.log(this.persondata,1111111)
			}
		},
		methods: {
			bindDateChange: function(e) {
				this.date = e.detail.value;
				this.persondata[0].birthday = e.detail.value
				console.log(e.detail)
			},
			radiochecked(e){
				console.log(e.detail.value)
				this.persondata[0].gender = e.detail.value
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},

			update() {
				let that = this

				let data = 
				
				 console.log(isEmpytInput(this.persondata),this.persondata)
			}
		},

		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
	}
</script>

<style scoped>
	.box {
		width: 100%;
		height: 100%;
		background-color: #F0F0F0;
	}

	.xian {
		width: 97%;
		height: 1rpx;
		margin: 0 auto;
		background-color: #F0F0F0;
	}

	.top {
		width: 97%;
		height: 450rpx;
		float: left;
		position: relative;
		background-color: #FFFFFF;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		top: 15rpx;
		left: 50%;
		transform: translateX(-50%);
		border-radius: 15rpx;
	}

	.img_box {
		width: 200rpx;
		height: 200rpx;
		background-color: #007860;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.img_box image {
		width: 97%;
		height: 97%;
		border-radius: 50%;
	}

	.top .name {
		margin-top: 15rpx;
		font-size: 1rem;
	}

	.update {
		width: 100%;
		float: left;
		margin-top: 25rpx;
	}

	.up_ul {
		width: 97%;
		margin: 0 auto;
		background-color: #FFFFFF;
		border-radius: 15rpx;
	}

	.up_li {
		width: 100%;
		line-height: 100rpx;

	}

	.up_li .up_name {
		width: 20%;
		height: 100%;
		text-align: center;
		float: left;
		font-size: 0.9rem;
	}

	.up_li .inp {
		width: 70%;
		height: 100rpx;
		float: left;
	}

	.up_li .inp input {
		width: 70%;
		height: 100rpx;
		box-sizing: border-box;
		padding-left: 15rpx;
	}

	.savedata {
		width: 100%;
		height: 90rpx;
		float: left;
		margin-top: 30rpx;
	}

	.savedata .but_box {
		width: 90%;
		height: 100%;
		margin: 0 auto;
	}

	.but_box button {
		background-color: #007860;
		color: #FFFFFF;
	}
</style>
